package com.raccoonagents.tools.cascet2d.model.components;

import org.lwjgl.opengl.GL11;

import com.raccoonagents.tools.cascet2d.model.Text;
import com.raccoonagents.tools.cascet2d.model.Vector2f;
import com.raccoonagents.tools.cascet2d.util.CalculationUtils;
import com.raccoonagents.tools.cascet2d.util.FontUtils;
import com.raccoonagents.tools.cascet2d.util.GraphicUtil;

public class LabeledButton extends AbstractButton {

	protected Text text;
	protected float [] currentBGColor;
	protected float [] onMouseOutBGColor = new float []{0.5f,0.5f,0.5f};
	protected float [] onMouseOverBGColor = new float []{1.0f,1.0f,1.0f};
	protected float [] onClickColor = new float []{0.5f,0.5f,0.5f};
	protected float [] onMouseOverTextColor = new float[]{1.0f,1.0f,1.0f};
	protected float [] onMouseOutTextColor = new float[]{0.0f,1.0f,0.0f};
	protected float opacity = 1.0f;
	protected boolean renderFrame = false;
	
	public Text getText() {
		return text;
	}

	public void setText(final Text text) {
		this.text = text;
	}

	public float[] getOnMouseOutBGColor() {
		return onMouseOutBGColor;
	}

	public void setOnMouseOutBGColor(final float[] onMouseOutBGColor) {
		this.onMouseOutBGColor = onMouseOutBGColor;
	}

	public float[] getOnMouseOverBGColor() {
		return onMouseOverBGColor;
	}

	public void setOnMouseOverBGColor(final float[] onMouseOverBGColor) {
		this.onMouseOverBGColor = onMouseOverBGColor;
	}

	public float[] getOnClickColor() {
		return onClickColor;
	}

	public void setOnClickColor(final float[] onClickColor) {
		this.onClickColor = onClickColor;
	}

	public float[] getOnMouseOverTextColor() {
		return onMouseOverTextColor;
	}

	public void setOnMouseOverTextColor(final float[] onMouseOverTextColor) {
		this.onMouseOverTextColor = onMouseOverTextColor;
	}

	public float[] getOnMouseOutTextColor() {
		return onMouseOutTextColor;
	}

	public void setOnMouseOutTextColor(final float[] onMouseOutTextColor) {
		this.onMouseOutTextColor = onMouseOutTextColor;
	}

	public void setOpacity(final float opacity) {
		this.opacity = opacity;
	}
	
	
	
	public boolean isRenderFrame() {
		return renderFrame;
	}

	public void setRenderFrame(final boolean renderFrame) {
		this.renderFrame = renderFrame;
	}

	public LabeledButton(final String text) {
		super(null);
		this.text = new Text(text);
		this.size = this.text.getSize();
		currentBGColor = onMouseOutBGColor;
		this.localLocation = new Vector2f(0.0f,0.0f);
		final Vector2f adjustText = CalculationUtils.getCenteredPosition(this.getSize(), this.text.getSize());
		this.text.setLocation(adjustText);
		this.text.setParentElement(this);
		
	}
	
	@Override
	public void onClick() {
		currentBGColor = onClickColor;		
		text.setColor(onMouseOutTextColor);
	}

	@Override
	public void onMouseOut() {
		currentBGColor = onMouseOutBGColor;
		text.setColor(onMouseOutTextColor);		
	}

	@Override
	public void onMouseOver() {
		currentBGColor = onMouseOverBGColor;
		text.setColor(onMouseOverTextColor);

	}

	
	
	@Override
	public void render() {
		if(!visible){
			return;
		}
		if(renderFrame){
			GraphicUtil.renderRectangle(this);			
		}
		
		GL11.glPushMatrix();
		GL11.glTranslatef(localLocation.getX(), localLocation.getY(), 0.0f);		
		// final FloatBuffer fb = FloatBuffer.allocate(16);
		GL11.glRotatef(localRotation,0.0f,0.0f, 1.0f);
		//GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, fb);
		FontUtils.renderText(text);
		GL11.glPopMatrix();
	}

	@Override
	public float[] getColor() {
		return currentBGColor;
	}

	@Override
	public float getOpacity() {
		return opacity;
	}
	
	/*
	@Override
	public float getRotation() {
		return parent.getWorldRotation(localRotation);
	}*/
	
	@Override
	public Vector2f getSize() {
		return text.getSize();
	}
	
	@Override
	public void onPress() {
		currentBGColor = onClickColor;
		text.setColor(onMouseOutTextColor);		
	}

}
